﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Dynamic;

using Tango.Dynamic;
using Tango.Scripting;

namespace Tango.ExpressionCompiler.DynamicExpression.Binders
{
	class ToBinaryBinder : BinderBase
	{
		public override Type ReturnType
		{
			get{return typeof(bool);}
		}

		public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
		{
			var restrictions=BindingRestrictions.Empty.AndLimitType(target);
			var value=target.GetLimitedExpression();

			Expression expression=value;

			if(value.Type!=typeof(bool) && value.Type.IsValueType==false)
			{
				// For reference types do a test for null
				expression=Expression.Equal(value,Expression.Constant(null));
			}
			else
			{
				expression=this.ThrowException("ToBinaryBinder: could not convert expression to boolean");
			}

			return new DynamicMetaObject(expression,restrictions);
		}
	}
}
